//
//  FZHomeStatusListViewModel.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/24/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZHomeStatusListViewModel: UIView {
    
    
    lazy var statusList: [FZHomeStatusViewModel] = [FZHomeStatusViewModel]()
    
    
    
    
    
    
}
//MARK:==========================得到数据==========================
extension FZHomeStatusListViewModel{
    /**
     - parmeter refreshType: 刷新的方式PUSH下拉，PULL 上拉
     - parameter newNessages: -2：错误消息，-1：上拉加载更多信息；大于等于0下来记载更多
     - parameter complete: 完成状态
     */
    func loadStatListData(refreshType: FZHomeRefreshType, complete:(newMessages: Int, isComplete: Bool)->()){
        
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
        if refreshType == .PUSH {
            max_id = statusList.last?.status?.id ?? 0
            if max_id > 0 {
                max_id -= 1
            }
        }else{
            
            since_id = statusList.first?.status?.id ?? 0
        }
        FZNetworking.sharedTools.homeLoadStatusData(since_id, max_id: max_id) { (responseObject, error) in
            if error != nil{
                print("ERROR:  loadStatListData-->\(error)")
                complete(newMessages: -2,isComplete: false)
                return
            }
            
            guard let responese = responseObject else{
                complete(newMessages: -2,isComplete: false)
                return
            }
            let statusList = responese["statuses"] as! [[String: AnyObject]]
            var tempArray:[FZHomeStatusViewModel] = [FZHomeStatusViewModel]()
            for value in statusList{
                
                tempArray.append(FZHomeStatusViewModel(status: FZHomeStatus(dic: value)))
                
            }
            if refreshType == .PUSH{
                
                self.statusList = self.statusList + tempArray
                complete(newMessages: -1, isComplete: true)
                
            }else{
                self.statusList = tempArray + self.statusList
                
                let newMessages = self.getNewMessages()
                complete(newMessages: newMessages, isComplete: true)
            }
            
        }
    }
    
    
    //得到最新消息的数量
    func getNewMessages() -> Int {
        
        var newMessages = 0
        //得到当前数组中的最大ID
        let maxID = self.statusList.first?.status?.id ?? 0
        //得到保存在偏好设置中的最大ID
        let oldMax = getSinaMaxID()
        
        if maxID > oldMax{
            
            for status in statusList {
                
                let id = (status.status?.id)!
                
                if id > oldMax {
                    
                    newMessages += 1
                }
            }
        }
        saveSinaMaxID(maxID)
        return newMessages
    }
    
    //从沙盒中得到最大的id
    func  getSinaMaxID() -> Int64 {
        
        let maxID = NSUserDefaults.standardUserDefaults().objectForKey(sinaMaxID) as? String
        
        guard let max = maxID else{
            return 0
        }
        
        return Int64(max)!
        
    }
    //保存最大的id
    func saveSinaMaxID(maxID: Int64) {
        
        NSUserDefaults.standardUserDefaults().setObject("\(maxID)", forKey: sinaMaxID)
        NSUserDefaults.standardUserDefaults().synchronize()
    }
    
}
